import validate from 'wechat-validate'
import http from '../../../utils/http'
Page({
  behaviors: [validate],
  data: {
    id: '', // 查询与修改所需id
    houseId: '',
    houseInfo: '',
    repairItemId: '',
    repairItemName: '',
    mobile: '', //电话
    appointment: '', // 预约日期
    description: '', //问题描述
    attachment: [], // 附件数组
    // { url: '/repair_pkg/static/uploads/attachment.jpg' },
    // { url: '/repair_pkg/static/uploads/attachment.jpg' },
    // currentDate: new Date().getTime(),
    currentDate: Date.now(),
    houseLayerVisible: false, // 房屋遮罩层
    repairLayerVisible: false,// 修理遮罩层
    dateLayerVisible: false,
    houseList: [
      { name: '北京西三旗花园1号楼 101' },
      { name: '北京东村家园3号楼 302' },
      { name: '北京育新花园3号楼 703' },
      { name: '北京天通苑北苑8号楼 403' },
    ],
    // repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    repairItem:[]
  },
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  onLoad({id}){
   this.setData({id})
   this.getRepairInfo()
  },
  async getRepairInfo(){
    const { data } = await wx.http({
      url: '/repair/'+ this.data.id,
      method: 'GET'
    })
    // 数据回显
    // 如果有，后面覆盖前面
    // const allData = {...this.data,...data}
    this.setData({
      ...data
    })
  },
  // 报修房屋请求
  async openHouseLayer() {
    const res = await wx.http({
      url: '/house',
    })
    // console.log(1111);
    if (!res.data.length) {
      return wx.utils.toast('您暂无审核通过的房屋！')
    }
    this.setData({ houseList: res.data, houseLayerVisible: true })
  },
  // 报修选择
  onSelectLayer(e){
   this.setData({
    houseId: e.detail.id,
    houseInfo: e.detail.name,
   })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 维修项目
  async openRepairLayer() { 
    // 长度存在
    if(this.data.repairItem.length){
      return this.setData({repairLayerVisible: true })
    }
    const {data:repairItem} = await wx.http.get('/repairItem')
    this.setData({repairItem, repairLayerVisible: true })
  },
  onSelectRepairLayer(e){
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name,
    })
  },
  // 时间的确定事件,有问题
  onConfirmDataPick(e){
   this.setData({
     appointment: wx.utils.formDateToString(e.detail),
     dateLayerVisible: false,
   })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 上传附件
  async uploadAttachment(e){
    const tempFilePathList = e.detail.file.map(v=>v.tempFilePath)
    const promiseList = tempFilePathList.map(v => wx.http.upload(v,'repair_attachment'))
    // Promise.all 
    const res = await Promise.all(promiseList)
    const attachment = res.map( v => v.data)
    // console.log('附件路径',attachment);
    this.setData({
      attachment: [...this.data.attachment,...attachment]
    })
  },
  deleteImg(e){
    // console.log(e.detail.index); 
    // 删除图片
    this.data.attachment.splice(e.detail.index,1)
   this.setData({
    attachment: this.data.attachment
   })
  },
  // 表单校验，提交报修
  async goList() {
    if(!this.validate()) return
    // 这里要写 id
    const {id,houseId,repairItemId,mobile,appointment,description,attachment } = this.data
    const res = await wx.http({
      url: '/repair',
      method: 'POST',
      data: {id,houseId,repairItemId,mobile,appointment,description,attachment } 
    })
   let currentPages = getCurrentPages()
   // 这里页面栈是四个，为什么最后是走的长度==3的判断
  // 统一跳转到报修页面
  if(currentPages.length === 2){
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  }else if(currentPages.length === 3){
    // 回转一个页面
    wx.navigateBack(2)
  }else{
    // 如果是其他的页面跳进来的
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  }
  // 关于路径问题！此处写的不是以根路径开始的绝对路径？？而是以分包开始的
    // wx.reLaunch({
    //   url: '/repair_pkg/pages/list/index',
    // })
  },
})
